package com.p595263720.o_amap.map;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.route.RouteSearch;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.p595263720.o_amap.map.models.InfoWinAdapterForLSJ;
import com.p595263720.o_amap.map.models.UnifiedMarkerOptions;
import com.p595263720.o_amap.map.models.UnifiedMyLocationStyle;
import com.p595263720.o_amap.map.models.UnifiedPolylineOptions;
import com.p595263720.o_amap.map.models.UnifiedShowAreaOptions;
import com.p595263720.o_amap.map.models.UnifiedUiSettings;


import java.util.ArrayList;
import java.util.List;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MapHandlers implements MethodChannel.MethodCallHandler{
    private Context mContext;
    private AMap mAMap;
    private MethodChannel.Result result;
    private Gson gson;

    public MapHandlers(Context context, AMap map) {
        this.mAMap = map;
        this.mContext = context;
        gson = new Gson();
        creatInfowwindow();
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        this.result = result;
        switch (call.method) {
            case "setMapType":
                setMapType(call);
                break;
            case "setZoomLevel":
                setZoomLevel(call);
                break;
            case "setPosition":
                setPostion(call);
                break;
            case "changeLatLng":
                changeLatLng(call);
                break;
            case "getCenterPoint":
                getCenterPoint();
                break;
            case "showIndoorMap":
                showIndoorMap(call);
                break;
            case "setUiSettings":
                setUiSettings(call);
                break;
            case "setMyLocationStyle":
                setMyLocationStyle(call);
                break;
            case "addMarker":
                addMarker(call);
                break;
            case "addMarkers":
                addMarkers(call);
                break;
            case "clearMarkers":
                clearMarkers(call);
                break;
            case "clearMap":
                clearMap(call);
                break;
            case "addPolyline":
                addPolyline(call);
                break;
            case "setPointToCenter":
                setPointToCenter(call);
                break;
            case "setShowArea":
                setShowArea(call);
                break;
        }
    }

    // 设置地图显示模式
    public void setMapType(MethodCall call) {
        int mayType = call.argument("mapType");
        mAMap.setMapType(mayType);
        this.result.success(null);
    }

    //设置地图缩放等级
    public void setZoomLevel(MethodCall call) {
        int level = call.argument("zoomLevel");
        mAMap.moveCamera(CameraUpdateFactory.zoomTo(level));
        this.result.success(null);
    }

    // 设置中心点
    public void setPostion(MethodCall call) {
        String latlng = call.argument("target");
        LatLng target = new Gson().fromJson(latlng, LatLng.class) != null ? new Gson().fromJson(latlng, LatLng.class) : new LatLng(39.941711, 116.382248);
        double zoom = call.argument("zoom") != null ? (double) call.argument("zoom") : 10.0;
        double tilt = call.argument("tilt") != null ? (double) call.argument("tilt") : 0.0;
        double bearing = call.argument("bearing") != null ? (double) call.argument("bearing") : 0.0;
        this.mAMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(target, (float) zoom, (float) tilt, (float) bearing)));
        this.result.success(null);
    }

    // 平移动到中心位置
    public void changeLatLng(MethodCall call) {
        String latlng = call.argument("target") != null ? (String) call.argument("target") : "{}";
        LatLng target = new Gson().fromJson(latlng, LatLng.class);
        this.mAMap.animateCamera(CameraUpdateFactory.changeLatLng(target));
        this.result.success(null);
    }

    // 获取中心点位置坐标
    public void getCenterPoint() {
        LatLng target = this.mAMap.getCameraPosition().target;
        this.result.success(JSON.toJSONString(target));
    }

    // 设置是否显示室内地图
    public void showIndoorMap(MethodCall call) {
        boolean enable = call.argument("showIndoorMap");
        this.mAMap.showIndoorMap(enable);
        this.result.success(null);
    }

    //设置地图初始化参数UiSetting
    public void setUiSettings(MethodCall call) {
        String uiSettings = call.argument("uiSettings");
        gson.fromJson(uiSettings, UnifiedUiSettings.class).applyTo(this.mAMap);
        this.result.success(null);
    }

    //这是当前位样式
    public void setMyLocationStyle(MethodCall call) {
        String myLocationStyle = call.argument("myLocationStyle");
        gson.fromJson(myLocationStyle, UnifiedMyLocationStyle.class).applyTo(this.mAMap);
        this.result.success(null);
    }

    // 添加mark 可添加图片或者自定义widget
    public void addMarker(MethodCall call) {
        try {
            String options = call.argument("markerOptions");
            gson.fromJson(options, UnifiedMarkerOptions.class).applyTo(this.mAMap, mContext);
            this.result.success(null);
        } catch (Exception e) {
        }

    }

    // 批量添加markers
    public void addMarkers(MethodCall call) {
        boolean isMoveTocenter = call.argument("moveToCenter") != null ? (boolean) call.argument("moveToCenter") : true;
        String optionsListJson = call.argument("markerOptionsList") != null ? (String) call.argument("markerOptionsList") : "[]";
        List<UnifiedMarkerOptions> list = gson.fromJson(optionsListJson, new TypeToken<List<UnifiedMarkerOptions>>() {
        }.getType());
        ArrayList<MarkerOptions> markerOptions = new ArrayList<MarkerOptions>();
        for (UnifiedMarkerOptions unifiedMarkerOptions : list) {
            markerOptions.add(unifiedMarkerOptions.toMarkerOption(mContext));
        }
        List<Marker> markerList = this.mAMap.addMarkers(markerOptions, isMoveTocenter);
        for (int i = 0; i < markerList.size(); i++) {
            if (list.get(i).extData != null) {
                markerList.get(i).setObject(list.get(i).extData);
                markerList.get(i).showInfoWindow();
            }

        }
        this.result.success(null);
    }

    // 删除所有marker 参数为true,myLocationOverlay（内置定位覆盖物）除外。
    public void clearMarkers(MethodCall call) {
        List<Marker> list = this.mAMap.getMapScreenMarkers();
        boolean isKeepMyLocationOverlay = call.argument("isKeepMyLocationOverlay") != null ? (boolean) call.argument("isKeepMyLocationOverlay") : true;
        for (Marker mark : list) {
            if (isKeepMyLocationOverlay) {
                if (mark.getObject() != null) {
                    mark.remove();
                }
            } else {
                mark.remove();
            }
        }
        this.result.success(null);
    }

    //从地图上删除所有的覆盖物(marker，circle，polyline 等对象),参数为true,myLocationOverlay（内置定位覆盖物）除外。
    public void clearMap(MethodCall call) {
        boolean isKeepMyLocationOverlay = call.argument("isKeepMyLocationOverlay") != null ? (boolean) call.argument("isKeepMyLocationOverlay") : true;
        this.mAMap.clear(isKeepMyLocationOverlay);
        this.result.success(null);
    }

    // 添加mark 可添加图片或者自定义widget
    public void addPolyline(MethodCall call) {
        String options = call.argument("options");
        gson.fromJson(options, UnifiedPolylineOptions.class).applyTo(this.mAMap);
        this.result.success(null);
    }

    // 设置屏幕上的某个像素点为地图中心点
    public void setPointToCenter(MethodCall call) {
        double x = call.argument("x");
        double y = call.argument("y");
        mAMap.setPointToCenter((int) x, (int) y);
        this.result.success(null);
    }


    // 设置屏幕上的某个像素点为地图中心点
    public void setShowArea(MethodCall call) {
        String options = call.argument("options");
        gson.fromJson(options, UnifiedShowAreaOptions.class).applyTo(this.mAMap);
        this.result.success(null);
    }

    public void creatInfowwindow() {
        mAMap.setInfoWindowAdapter(new InfoWinAdapterForLSJ(mContext));
    }

}
